package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.Material;
import com.mt.roll.core.dal.service.MaterialServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.params.MaterialParam;
import com.mt.roll.wms.params.MaterialQueryParam;
import com.mt.roll.wms.service.MaterialService;
import com.mt.roll.wms.vo.MaterialVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author weihongyang
 * @Date 2020/12/30 14:01
 * @Version 1.0
 */
@Service
@Transactional
public class MaterialServiceImpl extends BaseService implements MaterialService {
    @Autowired
    private MaterialServiceBiz materialServiceBiz;


    /**
     * 获取xx
     *
     * @param idParam 主键参数
     * @return xx
     */
    @Override
    public R<MaterialVo> get(IdParam idParam) {
        Material material = materialServiceBiz.getById(idParam.getId());
        Assert.notNull("物料不存在", material);
        MaterialVo materialVo = MaterialVo.builder().build();
        BeanUtils.copyProperties(material, materialVo);
        return successful(materialVo);
    }

    /**
     * 获取xx列表
     *
     * @param materialQueryParam xx查询参数
     * @return xx列表
     */
    @Override
    public R<List<MaterialVo>> list(MaterialQueryParam materialQueryParam) {
        return null;
    }

    /**
     * 获取xx分页列表
     *
     * @param materialQueryParam xx查询参数
     * @return xx分页列表
     */
    @Override
    public R<PageVo<MaterialVo>> page(MaterialQueryParam materialQueryParam) {
        QueryWrapper<Material> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(materialQueryParam.getEnabled()), Material.ENABLED, materialQueryParam.getEnabled())
                .like(StringUtils.isNotBlank(materialQueryParam.getCode()), Material.CODE, materialQueryParam.getCode())
                .or()
                .like(StringUtils.isNotBlank(materialQueryParam.getName()), Material.NAME, materialQueryParam.getName())
                .orderByDesc(Material.CREATE_TIME);
        IPage<Material> page = materialServiceBiz.page(new Page<>(materialQueryParam.getCurrent(), materialQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, MaterialVo.class));
    }

    /**
     * 获取xx树列表
     *
     * @param materialQueryParam xx查询参数
     * @return xx树列表
     */
    @Override
    public R<List<MaterialVo>> tree(MaterialQueryParam materialQueryParam) {
        return null;
    }

    /**
     * 新增xx
     *
     * @param materialParam xx参数
     * @return 主键
     */
    @Override
    public R<IdVo> add(MaterialParam materialParam) {
        QueryWrapper<Material> wrapper = new QueryWrapper<>();
        wrapper.eq(Material.CODE, materialParam.getCode());
        Assert.eqZero(materialServiceBiz.count(wrapper), "物料编码已经存在");
        QueryWrapper<Material> wrapperName = new QueryWrapper<>();
        wrapperName.eq(Material.NAME, materialParam.getName());
        Assert.eqZero(materialServiceBiz.count(wrapperName), "物料名称已经存在");
        Material material = new Material();
        BeanUtils.copyProperties(materialParam, material);
        setCommonField(material);
        materialServiceBiz.save(material);
        return successful(IdVo.builder().id(material.getId()).build());
    }

    /**
     * 更新xx
     *
     * @param materialParam xx参数
     * @return 主键
     */
    @Override
    public R<IdVo> update(MaterialParam materialParam) {
        Material material = materialServiceBiz.getById(materialParam.getId());
        //修改物料编码需要检验新编码是否已存在
        if (!material.getCode().equals(materialParam.getCode())) {
            QueryWrapper<Material> wrapper = new QueryWrapper<>();
            wrapper.eq(Material.CODE, materialParam.getCode());
            Assert.eqZero(materialServiceBiz.count(wrapper), "物料编码已经存在");
        }
        Material updateMaterial = new Material();
        BeanUtils.copyProperties(materialParam, updateMaterial);
        setUpdateCommonField(updateMaterial);
        materialServiceBiz.updateById(updateMaterial);
        return successful(IdVo.builder().id(material.getId()).build());
    }

    /**
     * 启用停用
     *
     * @param enabledParam 启停参数
     * @return 主键
     */
    @Override
    public R<IdVo> enabled(EnabledParam enabledParam) {
        return null;
    }

    /**
     * 删除xx
     *
     * @param idParam 主键参数
     * @return 主键
     */
    @Override
    public R<IdVo> delete(IdParam idParam) {
        Material material = materialServiceBiz.getById(idParam.getId());
        if (material == null) {
            return failed("物料不存在");
        }
        //使用更新方式删除，需要把code设置为历史code（后面跟随机数）
        Material updateMaterial = new Material().setId(material.getId());
        updateMaterial.setCode(material.getCode() + "_" + RandomUtils.nextInt());
        updateMaterial.setValid(WhetherEnum.NO.getValue());
        setUpdateCommonField(updateMaterial);
        materialServiceBiz.updateById(updateMaterial);
        //更新时逻辑删除字段没有更新，需要再调用一次删除操作
        materialServiceBiz.removeById(updateMaterial.getId());
        return successful(IdVo.builder().id(material.getId()).build());
    }

    /**
     * 批量删除xx
     *
     * @param idListParam 主键列表参数
     * @return 主键列表
     */
    @Override
    public R<IdListVo> delete(IdListParam idListParam) {
        return null;
    }

    @Override
    public R<String> getCode() {
        return successful(CodeGeneratorHelper.getMaterialCode());
    }
}
